package GUI;

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/*
 * ExemploJTable.java
 *
 * Created on 25 de Maio de 2004, 08:30
 */

/**
 * 
 * @author Almedson Ferreira
 */
public class ExemploJTable extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form ExemploJTable */

	private JComboBox scmbSexo;
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JPanel bAdd;
	private JButton bDelete;
	private JButton bMais;
	private JButton bMenos;
	private JButton jButton1;
	private JPanel pnTable;
	private JScrollPane scrollTable;
	private JTable table;

	// End of variables declaration//GEN-END:variables

	public ExemploJTable() {

		String[] sexos = { "M", "F" };
		scmbSexo = new JComboBox(sexos);
		initComponents();
		/**
		 * Este evento adiciona uma linha vazia na tabela
		 * 
		 */
		bMais.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) table
						.getModel();
				dtm.addRow(new Object[] { "", "", "", "", "" });
			}
		});

		/**
		 * Este evento exclui as linhas vazias que foram selecionadas na tabela
		 * 
		 */
		bMenos.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				int[] l = table.getSelectedRows();
				javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) table
						.getModel();

				for (int i = (l.length - 1); i >= 0; --i) {
					if ("".equals(table.getValueAt(l[i], 1).toString().trim())) {
						dtm.removeRow(l[i]);

					}
				}
			}
		});

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {// GEN-BEGIN:initComponents
		bAdd = new JPanel();
		pnTable = new JPanel();
		scrollTable = new JScrollPane();
		table = new JTable();
		bMenos = new JButton();
		bMais = new JButton();
		jButton1 = new JButton();
		bDelete = new JButton();

		setTitle("Exemplo JTable imasters");
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				exitForm(evt);
			}
		});

		bAdd.setLayout(null);

		pnTable.setLayout(new GridLayout(1, 0));

		pnTable.setBorder(new javax.swing.border.TitledBorder(
				"Exemplo de JTable"));
		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] { " ", "Nome", "Telefone","email", "Sexo" }) {
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(10);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setPreferredWidth(150);
		table.getColumnModel().getColumn(1).setResizable(true);
		table.getColumnModel().getColumn(2).setPreferredWidth(60);
		table.getColumnModel().getColumn(2).setResizable(true);
		table.getColumnModel().getColumn(3).setPreferredWidth(160);
		table.getColumnModel().getColumn(3).setResizable(true);
		table.getColumn(table.getColumnName(4)).setCellEditor(
				new javax.swing.DefaultCellEditor(scmbSexo));
		table.getColumnModel().getColumn(4).setPreferredWidth(30);
		table.getColumnModel().getColumn(4).setResizable(true);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
		scrollTable.setViewportView(table);

		pnTable.add(scrollTable);

		bAdd.add(pnTable);
		pnTable.setBounds(10, 10, 370, 230);

		bMenos.setText("-");
		bAdd.add(bMenos);
		bMenos.setBounds(400, 110, 40, 40);

		bMais.setText("+");
		bAdd.add(bMais);
		bMais.setBounds(400, 60, 40, 40);

		jButton1.setText("Add");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		bAdd.add(jButton1);
		jButton1.setBounds(390, 180, 60, 23);

		bDelete.setText("Del");
		bDelete.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bDeleteActionPerformed(evt);
			}
		});

		bAdd.add(bDelete);
		bDelete.setBounds(390, 210, 60, 23);

		getContentPane().add(bAdd, java.awt.BorderLayout.CENTER);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 474) / 2, (screenSize.height - 283) / 2,
				474, 283);
	}// GEN-END:initComponents

	private void bDeleteActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bDeleteActionPerformed
		// TODO add your handling code here:
		int[] l = table.getSelectedRows();
		javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) table
				.getModel();

		for (int i = (l.length - 1); i >= 0; --i)
			dtm.removeRow(l[i]);
	}// GEN-LAST:event_bDeleteActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) table
				.getModel();
		dtm.addRow(new Object[] { " ", "Almedson Ferreira", "1111111",
				"aferreira@agrovale.com", "M" });
	}// GEN-LAST:event_jButton1ActionPerformed

	/** Exit the Application */
	private void exitForm(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_exitForm
		System.exit(0);
	}// GEN-LAST:event_exitForm

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		new ExemploJTable().show();
	}
}